package com.tanhua.server.controller;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.tanhua.dubbo.server.vo.CommentType;
import com.tanhua.dubbo.server.vo.IdType;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.ErrorFlag;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import com.tanhua.server.vo.VisitorsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("movements")
public class QuanZiController {
    @Autowired
    private QuanZiService quanZiService;

    /**
     * 控制层实现功能:查看我的动态
     */
    @GetMapping("/all")
    public PageResult getMePublish(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "userId", required = true) Long userId) {
        PageResult pageResult = null;
        try {
            pageResult = quanZiService.findMeAllPublish(userId, page, pageSize);
            if (pageResult == null) {
                return new PageResult();
            }
        } catch (Exception e) {
            return null;
        }
        return pageResult;
    }


    /**
     * 控制层实现功能:获取圈子好友的动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult friendQuanZiPublish(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return quanZiService.friendQuanZiPublish(page, pageSize);

    }

    /**
     * 控制层实现功能:获取系统推荐的动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public PageResult recommendedQuanZiPublish(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return quanZiService.recommendedQuanZiPublish(page, pageSize);
    }


    /**
     * 控制层实现功能:发布动态
     */
    @PostMapping
    public ResponseEntity<Object> publishRelease(@RequestParam("textContent") String textContent,
                                               @RequestParam(value = "location", required = false) String location,
                                               @RequestParam(value = "latitude", required = false) String latitude,
                                               @RequestParam(value = "longitude", required = false) String longitude,
                                               @RequestParam(value = "imageContent", required = false) MultipartFile[] files
    ) {
        //发布动态,返回动态的id
        try {
            String publishId = quanZiService.savePublishRelease(textContent, location, latitude, longitude, files);
            if (StringUtils.isEmpty(publishId)) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorFlag.builder().errMessage("发布失败!").errCode("10001").build());
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorFlag.builder().errMessage("发布失败!").errCode("10001").build());

        }
        return ResponseEntity.ok(null);
    }


    /**
     * 控制层实现功能:点赞动态
     */
    @GetMapping("/{id}/like")
    public Long evaluationPublish(@PathVariable("id") String publishId) {
        Long count = null;
        try {
            count = quanZiService.likeAndLove(publishId, CommentType.LIKE, IdType.PUBLISH);
            if (count == null) {
                log.error("点赞失败!");
                return null;
            }
        } catch (Exception e) {
            log.error("点赞异常!");
            return null;
        }
        return count;
    }

    /**
     * 控制层实现功能:取消点赞动态
     */
    @GetMapping("/{id}/dislike")
    public Long evaluationCancelPublish(@PathVariable("id") String publishId) {
        Long count = null;
        try {
            count = quanZiService.likeCancelAndLove(publishId, CommentType.LIKE, IdType.PUBLISH);
            if (count == null) {
                log.error("点赞失败!");
                return null;
            }
        } catch (Exception e) {
            log.error("点赞异常!");
            return null;
        }
        return count;
    }


    /**
     * 控制层实现功能:喜欢动态
     */
    @GetMapping("/{id}/love")
    public Long evaluationLovePublish(@PathVariable("id") String publishId) {
        Long count = null;
        try {
            count = quanZiService.likeAndLove(publishId, CommentType.LOVE, IdType.PUBLISH);
            if (count == null) {
                log.error("喜欢失败!");
                return null;
            }
        } catch (Exception e) {
            log.error("喜欢异常!");
            return null;
        }
        return count;
    }

    /**
     * 控制层实现功能:取消喜欢动态
     */
    @GetMapping("/{id}/unlove")
    public Long evaluationCancelLovePublish(@PathVariable("id") String publishId) {
        Long count = null;
        try {
            count = quanZiService.likeCancelAndLove(publishId, CommentType.LOVE, IdType.PUBLISH);
            if (count == null) {
                log.error("喜欢失败!");
                return null;
            }
        } catch (Exception e) {
            log.error("喜欢异常!");
            return null;
        }
        return count;
    }


    /**
     * 查询单条动态
     */
    @GetMapping("/{id}")
    public ResponseEntity<QuanZiVo> getOnePublish(@PathVariable("id") String publishId) {
        QuanZiVo quanZiVo;
        try {
            quanZiVo = quanZiService.getOnePublish(publishId);
            if (quanZiVo == null) {
                return ResponseEntity.status(500).build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(quanZiVo);
    }


/*    *//**
     * TODO：谁看过我
     *
     * @return
     *//*
    @GetMapping("visitors")
    public ResponseEntity<Object> queryVisitors() {
        return ResponseEntity.ok(Collections.EMPTY_LIST);
    }*/


    /**
     * 查询所有的访客
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> getAllVisitors() {
        try {
            List<VisitorsVo> voList = quanZiService.queryVisitorsList();
            if (voList != null) {
                return ResponseEntity.ok(voList);
            }
        } catch (Exception e) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(new ArrayList<VisitorsVo>());
    }

}
